package com.boss.salary.service.impl;

import com.boss.asset.service.IAssetInfoService;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.DateUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.ApiElementConstant;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.ISalaryMeritPayDao;
import com.boss.salary.entity.SalaryMeritPay;
import com.boss.salary.service.ISalaryMeritPayService;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.utils.SysUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SalaryMeritPayServicelmpl implements ISalaryMeritPayService {
    @Autowired
    private ISalaryMeritPayDao salaryMeritPayDao;
    @Autowired
    private IAssetInfoService assetInfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSalaryMeritPay(SalaryMeritPay salaryMeritPay) {
        salaryMeritPay.setId(SysUtils.generateGuid());
        salaryMeritPay.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        salaryMeritPay.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        salaryMeritPay.setIsDeleted(SystemDictConstant.IS_FALSE);
        salaryMeritPayDao.insertSalaryMeritPay(salaryMeritPay);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSalaryMeritPay(String id, String mofDivCode) {
        salaryMeritPayDao.deleteSalaryMeritPay(id, mofDivCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSalaryMeritPay(SalaryMeritPay salaryMeritPay) {
        salaryMeritPay.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        salaryMeritPayDao.updateSalaryMeritPay(salaryMeritPay);
    }


    @Override
    public PageDTO<Map<String, Object>> querySalaryMeritPayPage(PaginationDTO paginationDTO, String mofDivCode, List<String> childrenId, String state) {
        // 根据 单位 状态 查询  mofDivCode区划 childrenId单位 state状态
        List<Map<String, Object>> agencyPersonRestrictPage = salaryMeritPayDao.querySalaryMeritPayPage(paginationDTO, mofDivCode, childrenId, state);
        // 根据 单位 状态 查询  mofDivCode区划 childrenId单位 state状态 查询条数
        int total = salaryMeritPayDao.querySalaryMeritPayPage(mofDivCode, childrenId, state);
        PageDTO<Map<String, Object>> pageDTO = new PageDTO<>();
        pageDTO.setRows(agencyPersonRestrictPage);
        pageDTO.setTotal(total);
        return pageDTO;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertUpdateSalaryMeritPay(List<SalaryMeritPay> salaryMeritPayList,UserDTO userDTO) {
        //通过agencyId删除
        salaryMeritPayDao.deleteSalaryMeritPays(salaryMeritPayList.get(0).getAgencyId(),userDTO.getProvince());
        List<Map<String, String>> getMerit=assetInfoService.getMerit("MERIT", ApiElementConstant.PROVINCE);
        //通过code查找当前工资类型
        //List<SalaryMeritPay> meritPay = salaryMeritPayDao.selectWageType(userDTO);
        //addList新增
        List<SalaryMeritPay> addList = new ArrayList<>();
        //将放回的信息循环遍历存入
        for (SalaryMeritPay salaryMeritPay : salaryMeritPayList) {
            //将工资类型循环
            for(Map map :getMerit ){
                //做匹配相同的code 存入工资id  工资name
                if(salaryMeritPay.getMeritPayCode().equals(map.get("code"))){
//                    salaryMeritPay.setMeritPayId(WageType.getMeritPayId());
                    salaryMeritPay.setMeritPayName(String.valueOf(map.get("name")));
                }
            }
            salaryMeritPay.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            salaryMeritPay.setIsDeleted(SystemDictConstant.IS_FALSE);
            salaryMeritPay.setId(SysUtils.generateGuid());
            addList.add(salaryMeritPay);
        }
        //存入
        if(addList.size()>0){
            salaryMeritPayDao.insertSalaryMeritPayPages(addList);
        }
    }

    @Override
    public List<SalaryMeritPay> queryAgencyIdList(String agencyId, UserDTO userDTO) {
        List<SalaryMeritPay> list = salaryMeritPayDao.queryAgencyIdList(agencyId, userDTO);
        return list;
    }


    @Override
    public List<SalaryMeritPay> agencyType(String province) {
        List<SalaryMeritPay> list = salaryMeritPayDao.agencyType(province);
        return list;
    }

    @Override
    public List<SalaryMeritPay> currentUnitList(String agencyId, UserDTO userDTO) {
        List<SalaryMeritPay> list = salaryMeritPayDao.currentUnitList(agencyId, userDTO);
        return list;
    }

    @Override
    public Integer queryCurrentNumber(String agencyId, String payCode) {
        Integer currentNumber = salaryMeritPayDao.queryCurrentNumber(agencyId,payCode);
        if(currentNumber==null){
            currentNumber=0;
        }
        return currentNumber;
    }

}
